﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MoiGioiHDBank.DTO;
using MoiGioiHDBank.DAO;

namespace MoiGioiHDBank
{
    public enum ThamSo
    {
        TimeOut = 600 // tính bằng giây
    }

    public partial class DangNhap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["sid"] != null)
            {
                this.lbKetQua.Text = (string)Request.Cookies["sid"].Value;
            }
        }

        protected void btDangNhap_Click(object sender, EventArgs e)
        {
            string kq = "";
            if (this.tbTenDangNhap.Text == "" || this.tbMatKhau.Text == "")
            {
                kq = "bạn chưa điền đủ thông tin!";
            }
            else
            {
                kq = Login(this.tbTenDangNhap.Text, this.tbMatKhau.Text);
            }
            this.lbKetQua.Text = kq;
        }

        public string Login(string userName, string password)
        {
            string kq = "Fail";
            Users us = UsersDAO.LayThongTinUser(userName, password);
            if (us != null)
            {
                SIDDTO sid = SIDDAO.LaySIDDTO(userName);
                if (sid != null && MineUtils.KiemTraTimeOut(sid.SID) == true) // co trong csdl va chua het timeout
                {
                    kq = sid.SID;
                }
                else
                {
                    kq = MineUtils.SIDGenerate();
                    SIDDAO.ThemSID(kq, userName,us.ThongTinKhachHang.MaNhomKhachHang,us.ThongTinKhachHang.SoThe);
                    Response.Cookies["sid"].Value = kq;
                    Response.Cookies["sid"].Expires = DateTime.Now.AddSeconds(600);
                }
            }
            else
            {
                kq = "not permission"; // sai username hoặc password
            }
            return kq;
        }
    }
}